﻿using DevExpress.XtraEditors.Controls;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    public partial class YeniAyrim : XPopupForm
    {

        public YeniAyrim()
        {
            InitializeComponent();
        }

        public void FaturaGirisModu()
        {
            userId = -1;
            lblGT.Visible = deGomlekTermini.Visible = false;
            lblTN.Visible = txtTerminNotu.Visible = false;
        }

        int userId;
        private void YeniAyrim_Load(object sender, EventArgs e)
        {
            userId = WhoAmI;

            if (isKD)
                userId = -1; //Tüm markalar

            // FaturaGirisModu();

            DataTable dt = Session.DataManager.ExecuteDataTable("GetMyBrands2", userId);
            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit riMyBrands = GetCustomGLE(dt, "Name", "BrandId");
            gleBrand.Properties.Assign(riMyBrands);

            gleSezon.Properties.Assign(RI(Rp.Sezon));

            //GetDetails("SODetay", "GetOrderRefByUser", userId, -1);

            gleOrderRef.Properties.Assign(GetIntellisensedRI(ds.SODetay));
            gleOrderRef.Properties.ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);
            gleOrderRef.Properties.View.Columns["Name"].Caption = "OrderRef.";
            gleOrderRef.Properties.View.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);
            gleOrderRef.Properties.View.Columns["BrandId"].Caption = "Marka";
            gleOrderRef.Properties.View.Columns["SezonId"].ColumnEdit = RI(Rp.Sezon);
            gleOrderRef.Properties.View.Columns["SezonId"].Caption = "Sezon";
            gleOrderRef.Closed += gleOrderRef_Closed;

            chkIdle.CheckedChanged += chkIdle_CheckedChanged;

            this.Shown += new EventHandler(YeniAyrim_Shown);

            gleBrand.MakeCompulsory(lblMarka, "Doldurulması zorunlu alan.");
            gleSezon.MakeCompulsory(lblSezon, "Doldurulması zorunlu alan.");
            gleOrderRef.MakeCompulsory(lblOrderRef, "Ayrılmış olduğu halde OrderRef. girmeden devam edemezsiniz.");
            chkIdle.CheckedChanged += new EventHandler(chkIdle_CheckedChanged);
        }

        public bool Ayrilmis
        {
            get { return !chkIdle.Checked; }
        }

        private void gleBrand_EditValueChanged(object sender, EventArgs e)
        {
            ds.SODetay.Clear();
            GetDetails("SODetay", "GetOrderRefByUser", userId, BrandId);
            gleOrderRef.Properties.View.BestFitColumns();
            ValidateAll(false);
        }

        private void gleSezon_EditValueChanged(object sender, EventArgs e)
        {
            ValidateAll(false);
        }

        void chkIdle_CheckedChanged(object sender, EventArgs e)
        {
            if (Ayrilmis)
                gleOrderRef.MakeCompulsory(lblOrderRef, "Ayrılmış olduğu halde OrderRef. girmeden devam edemezsiniz.");
            else
                gleOrderRef.MakeNOTCompulsory();

            gleOrderRef.Enabled = Ayrilmis;

            if (Ayrilmis)
                gleOrderRef.Focus();
            else
            {
                gleOrderRef.EditValue = null;
                gleBrand.Focus();
            }

            ValidateAll(false);
        }

        void gleOrderRef_Closed(object sender, ClosedEventArgs e)
        {
            gleSezon.EditValueChanged -= gleSezon_EditValueChanged;
            gleBrand.EditValueChanged -= gleBrand_EditValueChanged;

            if (ds.SODetay.FindByName(gleOrderRef.EditValue.ToStr()) != null)
            {
                ConfExpert.Datasets.MyData.SODetayRow row = ds.SODetay.FindByName(gleOrderRef.EditValue.ToString());
                if (!row.IsBrandIdNull())
                    gleBrand.EditValue = row.BrandId;

                if (!row.IsSezonIdNull())
                    gleSezon.EditValue = row.SezonId;
            }

            gleSezon.EditValueChanged += gleSezon_EditValueChanged;
            gleBrand.EditValueChanged += gleBrand_EditValueChanged;

            ValidateAll(false);
        }

        void YeniAyrim_Shown(object sender, EventArgs e)
        {
            gleBrand.Focus();
        }

        public DataRow DataRow
        {
            get
            {
                DataTable dtAyrim = new DataTable();
                dtAyrim.Columns.Add("AyrimId", typeof(int));
                dtAyrim.Columns.Add("BrandId", typeof(int));
                dtAyrim.Columns.Add("SezonId", typeof(int));
                dtAyrim.Columns.Add("OrderRef", typeof(string));
                dtAyrim.Columns.Add("GomlekTermini", typeof(DateTime));
                dtAyrim.Columns.Add("TerminNotu", typeof(string));
                dtAyrim.Rows.Add(AyrimId, BrandId, SezonId, OrderRef, GomlekTermini, TerminNotu);
                return dtAyrim.Rows[0];
            }
        }

        public DataRow DataRow2
        {
            get
            {
                DataTable dtAyrim = new DataTable();
                dtAyrim.Columns.Add("AyrimId", typeof(int));
                dtAyrim.Columns.Add("Qty", typeof(int));
                dtAyrim.Columns.Add("GomlekTermini", typeof(DateTime));
                dtAyrim.Columns.Add("TerminNotu", typeof(string));
                dtAyrim.Rows.Add(AyrimId, null, GomlekTermini, TerminNotu);
                return dtAyrim.Rows[0];
            }
        }

        public string OrderRef
        {
            get
            {
                if (chkIdle.Checked)
                    return "";
                else return gleOrderRef.EditValue.ToStr();
            }
        }

        public int BrandId
        {
            get
            {
                return gleBrand.EditValue.ToInt();
            }
        }

        public int SezonId
        {
            get
            {
                return gleSezon.EditValue.ToInt();
            }
        }

        public object TerminNotu
        {
            get
            {
                if (txtTerminNotu.Text == "")
                    return DBNull.Value;
                else
                    return txtTerminNotu.Text;
            }
        }
        public object GomlekTermini
        {
            get
            {
                if (deGomlekTermini.EditValue == null)
                    return DBNull.Value;
                else
                    return (DateTime)deGomlekTermini.EditValue;
            }
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            if (deGomlekTermini.EditValue == null)
                ShowErrorMessage("Termin tarihi girmelisiniz");
            else
            {
                if (ValidateAll())
                {
                    AyrimId = ExecuteScalar("InsAyrimIdSelect", BrandId, SezonId, OrderRef).ToInt();
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
        }

        public int AyrimId { get; set; }
    }
}